﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using CustomsSystem.BLL;
using CustomsSystem.Model;

namespace WEB.customer
{
    public partial class ChildAdd : System.Web.UI.Page
    {
        CustomerManage cusManage = new CustomerManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCodeData(ddlCustomerType, "clientType");
            }
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            
            #region 有效性判断
            string strErr = "";
            if (this.tbCusName.Text.Trim().Length == 0)
            {
                strErr += "  客户名不能为空！\\n";
            }

            if (cusManage.GetList("CustomerName ='" + tbCusName.Text.Trim() + "'").Tables[0].Rows.Count > 0)
            {
                MessageBox.Show(this, "该公司已经存在，请查看客户信息！");
                return;
            }//客户信息验证

            if (tbMark.Text.Trim().Length >= 999)
            {
                strErr += "  工作要求栏的字数超过长度！\\n";
            }
            if (tbAddr.Text.Trim().Length >= 99)
            {
                strErr += "  地址栏的字数超过长度！\\n";
            }//字数验证
            if (strErr.Trim() != "")
            {
                strErr = "注意\\n" + strErr;
                MessageBox.Show(this, strErr);
                return;
            }
            if (ddlInvtype.SelectedValue == "-1")
            {
                MessageBox.Show(this, "请选择发票类型！");
                return;
            }
            if (ddlCustomerType.SelectedValue == "-1")
            {
                MessageBox.Show(this, "请选择顾客类型！");
                return;
            }
            #endregion

            #region 数据录入
            Customer myCus = new Customer();
            myCus.Abbreviation = tbAbb.Text.Trim();
            myCus.CustomerNO = tbCusNo.Text.Trim();
            myCus.TaxNo = tbTaxNo.Text.Trim();
            //读取下拉框的值
            myCus.InvType = int.Parse(ddlInvtype.SelectedValue);
            myCus.Address = tbAddr.Text.Trim();
            myCus.PostCode = tbCode.Text.Trim();
            myCus.CustomerName = tbCusName.Text.Trim();
            myCus.Invoice = tbInv.Text.Trim();
            myCus.CustomerType = ddlCustomerType.Text.Trim();
            myCus.Remarks = tbMark.Text.Trim();
            //增加
            int id = cusManage.Add(myCus);
            if (id != 0)
            {
                MessageBox.Show(this, "增加客户成功，请点击完成");
            }//增加成功
            else
            {
                Response.Write("<script>alert('增加失败!')</script>");//可能是因为已存在该客户!
            }
            #endregion
        }

        /// <summary>
        /// 获取制定代码类型绑定到控件上
        /// </summary>
        /// <param name="ddl"></param>
        private void BindCodeData(DropDownList ddl, string codeType)
        {
            CodeManage codeBll = new CodeManage();
            List<Info_code> codeModels = codeBll.GetModelList("codeType='" + codeType + "'");
            foreach (Info_code aCode in codeModels)
            {
                ddl.Items.Add(new ListItem(aCode.codeName, aCode.codeID.ToString()));
            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {

        }
    }
}